<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/9/4
  Time: 15:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script type="text/javascript" src="dist/js/jquery/jquery.min.js"></script>
<html>
<head>
    <title>超级管理员</title>
    <style type="text/css">
        #id-div-permissionList{
            background-color: antiquewhite;
        }
        #id-div-roleList{
            background-color: antiquewhite;
        }
    </style>
</head>
<body>
2222
<hr>
<h2>${admin.maccount}</h2>
<h3>你拥有的角色:
    <c:forEach items="${admin.roleSet}" var="ars">
        ${ars.rname}
    </c:forEach>
</h3>
<hr>
<form id="id-form-permissionList" action="#">
    <h2>角色管理</h2>
    <div id="id-div-roleList">
        <%--<select name="rid" id="id-select-role" multiple="multiple" size="2">--%>
        <select id="id-select-role" name="rid" >
            <option value="rid2Num1">经理1</option>
            <option value="rid2Num2">经理2</option>
            <option value="rid2Num3">经理3</option>
        </select>
    </div>
    <hr>
    <h2>权限管理</h2>
    <div id="id-div-permissionList">
        <input type="checkbox" name="pid2Array" value="pid2Num1">111
        <input type="checkbox" name="pid2Array" value="pid2Num2">222
        <input type="checkbox" name="pid2Array" value="pid2Num3">333
    </div>
    <hr>
    <div>
        <input type="button" value="绑定">
        <input type="button" value="复位">
    </div>
</form>
<hr>



</body>
</html>
<script type="text/javascript">
    //http://localhost:8080/login.jsp
    //http://localhost:8080/member1_login?maccount=%E7%8E%8B%E5%A4%A7&mpwd=111

    /*444角色和新增的权限的绑定*/
    var bind2Rle2Permissions=function () {
        console.log("444调用了bind2Rle2Permissions函数");
        console.log($("#id-div-permissionList").find("input:checked"));
        var array2Permission2Checked=$("#id-div-permissionList").find("input:checked[name=pid2Array]");
        var str2pid2Array="";
        for (i=0;i<array2Permission2Checked.length;i++){
            console.log(array2Permission2Checked[i]);
            console.log($(array2Permission2Checked[i]).val());
            str2pid2Array+=$(array2Permission2Checked[i]).val()+",";
        }
        //复选框的多个值
        console.log("str2pid2Array===="+str2pid2Array);
        console.log($("select[name=rid]").val());

        $.ajax({
            type:"post",
            url:"role_update2Role2Permissons",
            data:{
                rid:$("select[name=rid]").val(),
                str2Pids:str2pid2Array,
            },
            dataType:"json",
            success:function (result) {
                console.log("result=====");
                console.log(result);
            }
        });



    }

    $("input[value=绑定]").click(function () {
        bind2Rle2Permissions();
    });



    /*333根据角色rid异步查询相应的 权限列表pid2Array
      333级联显示
    */
    var cascade2RolePermission=function () {
        console.log("333调用了cascade2RolePermission函数");
        console.log("333调用了$(select[name=rid]).val()======="+$("select[name=rid]").val());
        $.ajax({
            type:"post",
            url:"permission_searchByConditions",
            data:{
                pflag:0,
                //rid:2,
                rid:$("select[name=rid]").val(),
            },
            dataType:"json",
            success:function (result) {
                //console.log("result=====");
                //console.log(result);
                //console.log($("#id-div-permissionList").html());
                //$("#id-div-permissionList").find("input").removeAttr("checked");
                $.each(result,function (index,obj) {
                    //console.log("1111==="+obj);
                    //console.log(obj);
                    console.log(obj.pid);
                    //console.log($("#id-div-permissionList").find("input"));
                    //console.log($("#id-div-permissionList").find("input[value="+obj.pid+"]"));
                    $("#id-div-permissionList").find("input[value="+obj.pid+"]").attr("checked",true);//不起作用
                    $("#id-div-permissionList").find("input[value="+obj.pid+"]").prop("checked",true);
                });
            }
        });
    }
    $("select").click(function () {
        console.log("333标签Select的点击事件触发");
        $("#id-div-permissionList").find("input").removeAttr("checked");
        cascade2RolePermission();
    });


    /*222获取权限管理列表(pflag=0)*/
    var permissionList=function () {
        console.log("222调用了permissionList函数");
        $.ajax({
            type:"get",
            url:"permission_searchByConditions",
            data:{
                pflag:0,
                rid:-1,
            },
            dataType:"json",
            success:function (data) {
                //console.log("222==="+data);
                //console.log(data);
                var str="";
                var i=1;
                $.each(data,function (index,obj) {
                    //console.log(obj);
                    str+='<input  type="checkbox" name="pid2Array" value="'+obj.pid+'" />'+obj.pname;
                    if (i%4==0){
                        str+='</br>';
                    }
                    i++;
                });
                //console.log(str);
                $("#id-div-permissionList").html(str);
            }
        });
    }
    permissionList();

    /*111获取角色管理下拉框(rflag=0)*/
    var roleList=function () {
        console.log("111调用了roleList函数");
        $.ajax({
            type:"get",
            url:"role_searchByConditions",
            data:{
                rflag:-1,
            },
            dataType:"json",
            success:function (data) {
                //console.log(data);
                var str="";
                str+='<option value="-1">请选择角色</option>';
                $.each(data,function (index,obj) {
                    //console.log(obj);
                    str+='<option value="'+obj.rid+'">'+obj.rname+'</option>';
                });
                //console.log("======"+str);
                $("#id-select-role").html(str);
                //console.log($("#id-select-role").html());
            }
        });
    }
    roleList();


    console.log("000");
    $(function () {
        console.log("-111");
    });

</script>
